package io.lkt.modules.sys.controller;

import com.google.common.collect.Lists;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BArticleInfoEntity;
import io.lkt.modules.sys.entity.BArticleTempEntity;
import io.lkt.modules.sys.entity.BArticleTempLogEntity;
import io.lkt.modules.sys.entity.BBrandInfoEntity;
import io.lkt.modules.sys.service.BArticleInfoService;
import io.lkt.modules.sys.service.BArticleTempLogService;
import io.lkt.modules.sys.service.BArticleTempService;
import io.lkt.modules.sys.service.BBrandInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 文章库模板文章信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-03-05 10:02:05
 */
@RestController
@RequestMapping("/sys/barticletemp")
public class BArticleTempController extends AbstractController {
    @Autowired
    private BArticleTempService bArticleTempService;

    @Autowired
    BBrandInfoService bBrandInfoService;

    @Autowired
    private BArticleInfoService bArticleInfoService;

    @Autowired
    private BArticleTempLogService bArticleTempLogService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:barticletemp:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bArticleTempService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/lists")
//    @RequiresPermissions("sys:barticletemp:info")
    public R lists(@RequestBody BArticleTempEntity bArticleTemp) {

        List<BArticleTempEntity> bArticleTempLists = bArticleTempService.selectList(bArticleTemp);

        return R.ok().put("bArticleTempLists", bArticleTempLists);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:barticletemp:info")
    public R info(@PathVariable("id") Integer id) {
        BArticleTempEntity bArticleTemp = bArticleTempService.getById(id);

        return R.ok().put("bArticleTemp", bArticleTemp);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:barticletemp:save")
    public R save(@RequestBody BArticleTempEntity bArticleTemp) {

        if (bArticleTemp == null || bArticleTemp.getArticleTypeId() == null || bArticleTemp.getArticleTypeId() < 10) {
            return R.error("模板文章类型不能为空");
        }

        if (bArticleTemp == null || bArticleTemp.getArticleTitle() == null || bArticleTemp.getArticleTitle().trim().equals("")) {
            return R.error("模板文章标题不能为空");
        }

        if (bArticleTemp == null || bArticleTemp.getArticleDesc() == null || bArticleTemp.getArticleDesc().trim().equals("")) {
            return R.error("模板文章概述不能为空");
        }

        if (bArticleTemp == null || bArticleTemp.getArticleContent() == null || bArticleTemp.getArticleContent().trim().equals("")) {
            return R.error("模板文章内容不能为空");
        }

        bArticleTemp.setCreateBy(getUser().getUsername());
        bArticleTemp.setCreateTime(new Date());
        bArticleTemp.setUpdateBy(getUser().getUsername());
        bArticleTemp.setUpdateTime(new Date());

        bArticleTempService.save(bArticleTemp);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:barticletemp:update")
    public R update(@RequestBody BArticleTempEntity bArticleTemp) {
        ValidatorUtils.validateEntity(bArticleTemp);

        if (bArticleTemp == null || bArticleTemp.getArticleTypeId() == null || bArticleTemp.getArticleTypeId() < 10) {
            return R.error("模板文章类型不能为空");
        }

        if (bArticleTemp == null || bArticleTemp.getArticleTitle() == null || bArticleTemp.getArticleTitle().trim().equals("")) {
            return R.error("模板文章标题不能为空");
        }

        if (bArticleTemp == null || bArticleTemp.getArticleDesc() == null || bArticleTemp.getArticleDesc().trim().equals("")) {
            return R.error("模板文章概述不能为空");
        }

        if (bArticleTemp == null || bArticleTemp.getArticleContent() == null || bArticleTemp.getArticleContent().trim().equals("")) {
            return R.error("模板文章内容不能为空");
        }

        bArticleTemp.setUpdateBy(getUser().getUsername());
        bArticleTemp.setUpdateTime(new Date());

        bArticleTempService.updateById(bArticleTemp);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updateStatus")
//    @RequiresPermissions("sys:barticletemp:update")
    public R updateStatus(@RequestBody BArticleTempEntity bArticleTemp) {
        ValidatorUtils.validateEntity(bArticleTemp);

        bArticleTemp.setUpdateBy(getUser().getUsername());
        bArticleTemp.setUpdateTime(new Date());

        bArticleTempService.updateById(bArticleTemp);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:barticletemp:delete")
    public R delete(@RequestBody Integer[] ids) {
//        bArticleTempService.removeByIds(Arrays.asList(ids));

        for (Integer id : ids) {
            BArticleTempEntity bArticleTemp = new BArticleTempEntity();

            bArticleTemp.setId(id);
            bArticleTemp.setStatus(Integer.valueOf(0));
            bArticleTemp.setUpdateBy(getUser().getUsername());
            bArticleTemp.setUpdateTime(new Date());

            bArticleTempService.updateById(bArticleTemp);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/creatArticle")
//    @RequiresPermissions("sys:barticletemp:update")
    public R creatArticle(@RequestBody BArticleTempEntity bArticleTemp) {

        ValidatorUtils.validateEntity(bArticleTemp);

        if (bArticleTemp == null || bArticleTemp.getBrandName() == null || bArticleTemp.getIds() == null) {
            return R.error("缺少重要参数！");
        }

        BBrandInfoEntity bBrandInfoEntity = this.bBrandInfoService.queryByName(bArticleTemp.getBrandName());
        if (bBrandInfoEntity == null || bBrandInfoEntity.getId() == null) {
            return R.error("目标品牌不存在！");
        }

        Integer brandId = bBrandInfoEntity.getId();
        Integer[] bArticleTempIds = bArticleTemp.getIds();

        List<BArticleInfoEntity> bArticleInfoList = Lists.newArrayList();
        for (Integer bArticleTempId : bArticleTempIds) {
            // 模板文章查询
            BArticleTempEntity info = bArticleTempService.getById(bArticleTempId);

            // 模板文章生成文章
            BArticleInfoEntity bArticleInfo = new BArticleInfoEntity();
            bArticleInfo.setArticleTitle(info.getArticleTitle());
            if(info.getArticleTitle().contains("#BN#")){
                bArticleInfo.setArticleTitle(info.getArticleTitle().replaceAll("#BN#", bArticleTemp.getBrandName()));
            }
            bArticleInfo.setArticleDesc(info.getArticleDesc());
            if(info.getArticleDesc().contains("#BN#")){
                bArticleInfo.setArticleDesc(info.getArticleDesc().replaceAll("#BN#", bArticleTemp.getBrandName()));
            }
            bArticleInfo.setCreateUser(getUser().getUsername());
            bArticleInfo.setArticleContent(info.getArticleContent());
            bArticleInfo.setArticleImgUrl(info.getArticleImgUrl());
            bArticleInfo.setArticleBrandId(brandId);
            bArticleInfo.setCreateTime(new Date());
            bArticleInfo.setModifyTime(new Date());
            bArticleInfo.setArticleSource(Integer.valueOf(1));// 文章来源 1-平台添加 2-用户通过公众号添加 3-用户编辑的文章生成自己的文章
            bArticleInfo.setArticleStatus(Integer.valueOf(2));// 文章状态 0-删除 1-正常 2-隐藏
            bArticleInfo.setSpiderProcessStatus(Integer.valueOf(1));
            bArticleInfo.setSType("模板生成");
            bArticleInfo.setArticleLevel(Integer.valueOf(1)); // 经典文章 0-是 1-否
            bArticleInfo.setHtTemplateId(Integer.valueOf(1)); // 经典标题 0-是 1-否
            bArticleInfo.setArticleVersionNum(Integer.valueOf(2)); // 显示类型 0-大图 2- 常规
            bArticleInfo.setHotType(Integer.valueOf(0)); // 热度类型 0-随机 1-自定义

            boolean tip = this.bArticleInfoService.save(bArticleInfo);
            if (tip) {
                bArticleInfo.setArticleTypeName(bArticleTemp.getBrandName());
                bArticleInfoList.add(bArticleInfo);

                // 模板文章生成记录
                BArticleTempLogEntity bArticleTempLog = new BArticleTempLogEntity();

                bArticleTempLog.setTempId(info.getId());
                bArticleTempLog.setArticleId(bArticleInfo.getId());
                bArticleTempLog.setCreateBy(getUser().getUsername());
                bArticleTempLog.setCreateTime(new Date());
                bArticleTempLog.setUpdateBy(getUser().getUsername());
                bArticleTempLog.setUpdateTime(new Date());
                bArticleTempLog.setRemark("已入库");

                bArticleTempLogService.save(bArticleTempLog);
            }
        }

        return R.ok().put("bArticleInfoList", bArticleInfoList).put("bArticleBrandName", bBrandInfoEntity.getBrandName());
    }

}
